﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using DBService;

namespace YArchitech.Revit.MEP.Opening
{
	public class XmlUtils
	{
		public static bool AutoCreateCasing()
		{
			return Convert.ToBoolean(XmlUtils.GetOpeningSetNode("套管").Attributes["CreateCasing"].Value);
		}

		public static XmlNode GetOpeningSetNode(string sType)
		{
			XmlNode result = null;
			string filename = AssistFunc.GetAppRootPath() + "\\Data\\OpeningSet.xml";
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(filename);
			foreach (object obj in xmlDocument.SelectSingleNode("开洞设置"))
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == sType)
				{
					result = xmlNode;
				}
			}
			return result;
		}

		public static double GetCircleOutSize(CutType ct, HostType ht)
		{
			try
			{
				return new ConfigDataOperate.OfferConfigData(ConfigDataOperate.InitConfigDataFile()).CircelOpeningDist2Pipe;
			}
			catch
			{
			}
			return 0.0;
		}

		public static RectOutSize GetRectOutSize(CutType ct, HostType ht)
		{
			RectOutSize rectOutSize = new RectOutSize();
			rectOutSize.Left = (rectOutSize.Right = (rectOutSize.Top = (rectOutSize.Bottom = 0.0)));
			try
			{
				ConfigDataOperate.OfferConfigData offerConfigData = new ConfigDataOperate.OfferConfigData(ConfigDataOperate.InitConfigDataFile());
				rectOutSize.Left = (rectOutSize.Right = (rectOutSize.Top = (rectOutSize.Bottom = offerConfigData.RectOpeningDist2Pipe)));
			}
			catch
			{
			}
			return rectOutSize;
		}

		public static CasingType GetCastingType()
		{
			CasingType result = CasingType.Soft;
			XmlNode openingSetNode = XmlUtils.GetOpeningSetNode("套管");
			if (openingSetNode != null)
			{
				result = (CasingType)Convert.ToInt32(openingSetNode.Attributes["CasingType"].Value);
			}
			return result;
		}

		public static void CreateDataFile(string fileName, Autodesk.Revit.DB.Document doc)
		{
			if (!new FileInfo(fileName).Exists)
			{
				XmlDocument xmlDocument = new XmlDocument();
				XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement("", "协同数据", "");
				xmlDocument.AppendChild(xmlElement);
				foreach (string value in ConfigDataOperate.GetDocAndLinkDocFileName(doc))
				{
					XmlElement xmlElement2 = xmlDocument.CreateElement("项目");
					xmlElement.AppendChild(xmlElement2);
					XmlAttribute xmlAttribute = xmlDocument.CreateAttribute("文件名");
					xmlAttribute.Value = value;
					xmlElement2.Attributes.Append(xmlAttribute);
					foreach (XmlElement xmlElement3 in new List<XmlElement>
					{
						xmlDocument.CreateElement("有效"),
						xmlDocument.CreateElement("无效")
					})
					{
						xmlElement2.AppendChild(xmlElement3);
						XmlElement newChild2 = xmlDocument.CreateElement("墙洞");
						XmlElement newChild3 = xmlDocument.CreateElement("板洞");
						XmlElement newChild4 = xmlDocument.CreateElement("梁洞");
						XmlElement newChild5 = xmlDocument.CreateElement("组合洞");
						xmlElement3.AppendChild(newChild2);
						xmlElement3.AppendChild(newChild3);
						xmlElement3.AppendChild(newChild4);
						xmlElement3.AppendChild(newChild5);
					}
				}
				xmlDocument.Save(fileName);
			}
		}

		public static XmlNode GetXmlItemNodeByFileName(XmlDocument xmlDoc, string strDocFileName, bool bValidNode = true)
		{
			try
			{
				string str = string.Format("[@文件名='{0}']", strDocFileName);
				string xpath = "//协同数据/项目" + str + (bValidNode ? "/有效" : "/无效");
				return xmlDoc.SelectSingleNode(xpath);
			}
			catch
			{
			}
			return null;
		}

		public static string SplitPathName(string strPathName)
		{
			string result = "";
			try
			{
				int num = strPathName.LastIndexOf('\\');
				int num2 = strPathName.LastIndexOf('.');
				result = strPathName.Substring(++num, num2 - num);
			}
			catch
			{
			}
			return result;
		}

		public static bool IsCreateCasing(string fileName)
		{
			bool result = false;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "添加套管")
					{
						result = Convert.ToBoolean(xmlNode.Attributes["AutoCreate"].Value);
						break;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return result;
		}

		public static CasingType ReadCasingType(string fileName)
		{
			CasingType result = CasingType.Soft;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "添加套管")
					{
						result = (CasingType)Convert.ToInt32(xmlNode.Attributes["CasingType"].Value);
						break;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return result;
		}

		public static DbBeamToBeOpened ReadBeamOpening(Guid id, string fileName)
		{
			DbBeamToBeOpened result = null;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "梁洞")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							if (xmlNode2.Name == "Id-" + id)
							{
								return XmlUtils.ReadBeamOpening(xmlNode2);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return result;
		}

		public static DbWallToBeOpened ReadWallOpening(Guid id, string fileName)
		{
			DbWallToBeOpened result = null;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					foreach (object obj2 in ((XmlNode)obj))
					{
						foreach (object obj3 in ((XmlNode)obj2))
						{
							XmlNode xmlNode = (XmlNode)obj3;
							if (xmlNode.Name == "墙洞")
							{
								foreach (object obj4 in xmlNode.ChildNodes)
								{
									XmlNode xmlNode2 = (XmlNode)obj4;
									if (xmlNode2.Name == "Id-" + id)
									{
										return XmlUtils.ReadWallOpening(xmlNode2);
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return result;
		}

		public static DbFloorToBeOpened ReadFloorOpening(Guid id, string fileName)
		{
			DbFloorToBeOpened result = null;
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "板洞")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							if (xmlNode2.Name == "Id-" + id.ToString())
							{
								return XmlUtils.ReadFloorOpening(xmlNode2);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return result;
		}

		public static List<DbWallToBeOpened> ReadWallOpening(string fileName)
		{
			List<DbWallToBeOpened> list = new List<DbWallToBeOpened>();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					foreach (object obj2 in ((XmlNode)obj))
					{
						foreach (object obj3 in ((XmlNode)obj2))
						{
							XmlNode xmlNode = (XmlNode)obj3;
							if (xmlNode.Name == "墙洞")
							{
								foreach (object obj4 in xmlNode)
								{
									DbWallToBeOpened item = XmlUtils.ReadWallOpening((XmlNode)obj4);
									list.Add(item);
								}
								return list;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return list;
		}

		public static List<DbFloorToBeOpened> ReadFloorOpening(string fileName)
		{
			List<DbFloorToBeOpened> list = new List<DbFloorToBeOpened>();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					foreach (object obj2 in ((XmlNode)obj))
					{
						foreach (object obj3 in ((XmlNode)obj2))
						{
							XmlNode xmlNode = (XmlNode)obj3;
							if (xmlNode.Name == "板洞")
							{
								IEnumerator enumerator4 = xmlNode.ChildNodes.GetEnumerator();
								{
									while (enumerator4.MoveNext())
									{
										object obj4 = enumerator4.Current;
										DbFloorToBeOpened item = XmlUtils.ReadFloorOpening((XmlNode)obj4);
										list.Add(item);
									}
									break;
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return list;
		}

		public static List<DbBeamToBeOpened> ReadBeamOpening(string fileName)
		{
			List<DbBeamToBeOpened> list = new List<DbBeamToBeOpened>();
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					foreach (object obj2 in ((XmlNode)obj))
					{
						foreach (object obj3 in ((XmlNode)obj2))
						{
							XmlNode xmlNode = (XmlNode)obj3;
							if (xmlNode.Name == "梁洞")
							{
								IEnumerator enumerator4 = xmlNode.ChildNodes.GetEnumerator();
								{
									while (enumerator4.MoveNext())
									{
										object obj4 = enumerator4.Current;
										DbBeamToBeOpened item = XmlUtils.ReadBeamOpening((XmlNode)obj4);
										list.Add(item);
									}
									break;
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			return list;
		}

		public static DbWallToBeOpened ReadWallOpening(XmlNode xn0)
		{
			double num = Convert.ToDouble(xn0.Attributes["PtX"].Value);
			double num2 = Convert.ToDouble(xn0.Attributes["PtY"].Value);
			double num3 = Convert.ToDouble(xn0.Attributes["PtZ"].Value);
			int wallId = Convert.ToInt32(xn0.Attributes["WallId"].Value);
			int pipeId = Convert.ToInt32(xn0.Attributes["PipeId"].Value);
			double num4 = Convert.ToDouble(xn0.Attributes["Parameter1"].Value);
			double param = Convert.ToDouble(xn0.Attributes["Parameter2"].Value);
			int num5 = (int)Convert.ToDouble(xn0.Attributes["SectionType"].Value);
			OpeningStatus status = (OpeningStatus)Convert.ToInt32(xn0.Attributes["Status"].Value);
			string remark = Convert.ToString(xn0.Attributes["Remark"].Value);
			SectionParameter sp;
			if (num5 == 1)
			{
				sp = new SectionParameter(num4);
			}
			else
			{
				sp = new SectionParameter(num4, param);
			}
			double num6 = Convert.ToDouble(xn0.Attributes["BoxParameter1"].Value);
			double param2 = Convert.ToDouble(xn0.Attributes["BoxParameter2"].Value);
			double param3 = Convert.ToDouble(xn0.Attributes["BoxParameter3"].Value);
			double param4 = Convert.ToDouble(xn0.Attributes["BoxParameter4"].Value);
			BoxParameter bp;
			if ((int)Convert.ToDouble(xn0.Attributes["BoxSectionType"].Value) == 1)
			{
				bp = new BoxParameter(num6);
			}
			else
			{
				bp = new BoxParameter(num6, param2, param3, param4);
			}
			CutType cType = (CutType)Convert.ToInt32(xn0.Attributes["CutType"].Value);
			HostType hType = (HostType)Convert.ToInt32(xn0.Attributes["HostType"].Value);
			string level = Convert.ToString(xn0.Attributes["Level"].Value);
			DbWallToBeOpened dbWallToBeOpened = new DbWallToBeOpened(0, new XYZ(num, num2, num3), wallId, pipeId, sp, bp, cType, hType, level);
			dbWallToBeOpened.Remark = remark;
			dbWallToBeOpened.Status = status;
			dbWallToBeOpened.Guid = Guid.Parse(XmlUtils.getGuid(xn0.Name));
			dbWallToBeOpened.Date = DateTime.Parse(xn0.Attributes["Date"].Value);
			dbWallToBeOpened.DrivePipeTypeName = xn0.Attributes["DrivePipeTypeName"].Value;
			dbWallToBeOpened.AxisX = xn0.Attributes["AxisX"].Value;
			dbWallToBeOpened.AxisY = xn0.Attributes["AxisY"].Value;
			dbWallToBeOpened.MEPFileName = xn0.Attributes["MEPFileName"].Value;
			double num7 = Convert.ToDouble(xn0.Attributes["PipeStartPointX"].Value);
			double num8 = Convert.ToDouble(xn0.Attributes["PipeStartPointY"].Value);
			double num9 = Convert.ToDouble(xn0.Attributes["PipeStartPointZ"].Value);
			dbWallToBeOpened.PipeStartPoint = new XYZ(num7, num8, num9);
			double num10 = Convert.ToDouble(xn0.Attributes["PipeEndPointX"].Value);
			double num11 = Convert.ToDouble(xn0.Attributes["PipeEndPointY"].Value);
			double num12 = Convert.ToDouble(xn0.Attributes["PipeEndPointZ"].Value);
			dbWallToBeOpened.PipeEndPoint = new XYZ(num10, num11, num12);
			if (xn0.Attributes["OpeningID"] != null)
			{
				dbWallToBeOpened.OpeningID = new ElementId(Convert.ToInt32(xn0.Attributes["OpeningID"].Value));
			}
			if (xn0.Attributes["vtWallOrientX"] != null)
			{
				double num13 = Convert.ToDouble(xn0.Attributes["vtWallOrientX"].Value);
				double num14 = Convert.ToDouble(xn0.Attributes["vtWallOrientY"].Value);
				double num15 = Convert.ToDouble(xn0.Attributes["vtWallOrientZ"].Value);
				dbWallToBeOpened.vtWallOrient = new XYZ(num13, num14, num15);
			}
			dbWallToBeOpened.dWallWidth = Convert.ToDouble(xn0.Attributes["dWallWidth"].Value);
			dbWallToBeOpened.bEquipOpening = Convert.ToBoolean(xn0.Attributes["bEquipOpening"].Value);
			dbWallToBeOpened.dOpeningThickness = Convert.ToDouble(xn0.Attributes["dOpeningThickness"].Value);
			return dbWallToBeOpened;
		}

		public static DbFloorToBeOpened ReadFloorOpening(XmlNode xn0)
		{
			double num = Convert.ToDouble(xn0.Attributes["PtX"].Value);
			double num2 = Convert.ToDouble(xn0.Attributes["PtY"].Value);
			double num3 = Convert.ToDouble(xn0.Attributes["PtZ"].Value);
			int floorId = Convert.ToInt32(xn0.Attributes["FloorId"].Value);
			int pipeId = Convert.ToInt32(xn0.Attributes["PipeId"].Value);
			double num4 = Convert.ToDouble(xn0.Attributes["Parameter1"].Value);
			double param = Convert.ToDouble(xn0.Attributes["Parameter2"].Value);
			int num5 = (int)Convert.ToDouble(xn0.Attributes["SectionType"].Value);
			OpeningStatus status = (OpeningStatus)Convert.ToDouble(xn0.Attributes["Status"].Value);
			SectionParameter sp;
			if (num5 == 1)
			{
				sp = new SectionParameter(num4);
			}
			else
			{
				sp = new SectionParameter(num4, param);
			}
			double num6 = Convert.ToDouble(xn0.Attributes["BoxParameter1"].Value);
			double param2 = Convert.ToDouble(xn0.Attributes["BoxParameter2"].Value);
			double param3 = Convert.ToDouble(xn0.Attributes["BoxParameter3"].Value);
			double param4 = Convert.ToDouble(xn0.Attributes["BoxParameter4"].Value);
			string remark = Convert.ToString(xn0.Attributes["Remark"].Value);
			BoxParameter bp;
			if (Convert.ToInt32(xn0.Attributes["BoxSectionType"].Value) == 1)
			{
				bp = new BoxParameter(num6);
			}
			else
			{
				bp = new BoxParameter(num6, param2, param3, param4);
			}
			CutType cType = (CutType)Convert.ToInt32(xn0.Attributes["CutType"].Value);
			HostType hType = (HostType)Convert.ToInt32(xn0.Attributes["HostType"].Value);
			string level = Convert.ToString(xn0.Attributes["Level"].Value);
			DbFloorToBeOpened dbFloorToBeOpened = new DbFloorToBeOpened(0, new XYZ(num, num2, num3), floorId, pipeId, sp, bp, cType, hType, level);
			dbFloorToBeOpened.Status = status;
			dbFloorToBeOpened.Remark = remark;
			dbFloorToBeOpened.Guid = Guid.Parse(XmlUtils.getGuid(xn0.Name));
			dbFloorToBeOpened.Date = DateTime.Parse(xn0.Attributes["Date"].Value);
			dbFloorToBeOpened.DrivePipeTypeName = xn0.Attributes["DrivePipeTypeName"].Value;
			dbFloorToBeOpened.AxisX = xn0.Attributes["AxisX"].Value;
			dbFloorToBeOpened.AxisY = xn0.Attributes["AxisY"].Value;
			dbFloorToBeOpened.MEPFileName = xn0.Attributes["MEPFileName"].Value;
			double num7 = Convert.ToDouble(xn0.Attributes["PipeStartPointX"].Value);
			double num8 = Convert.ToDouble(xn0.Attributes["PipeStartPointY"].Value);
			double num9 = Convert.ToDouble(xn0.Attributes["PipeStartPointZ"].Value);
			dbFloorToBeOpened.PipeStartPoint = new XYZ(num7, num8, num9);
			double num10 = Convert.ToDouble(xn0.Attributes["PipeEndPointX"].Value);
			double num11 = Convert.ToDouble(xn0.Attributes["PipeEndPointY"].Value);
			double num12 = Convert.ToDouble(xn0.Attributes["PipeEndPointZ"].Value);
			dbFloorToBeOpened.PipeEndPoint = new XYZ(num10, num11, num12);
			if (xn0.Attributes["OpeningID"] != null)
			{
				dbFloorToBeOpened.OpeningID = new ElementId(Convert.ToInt32(xn0.Attributes["OpeningID"].Value));
			}
			dbFloorToBeOpened.PipeConnAxisXOnSectionPlaneAng = Convert.ToDouble(xn0.Attributes["PipeConnAxisXOnSectionPlaneAng"].Value);
			return dbFloorToBeOpened;
		}

		public static DbBeamToBeOpened ReadBeamOpening(XmlNode xn0)
		{
			double num = Convert.ToDouble(xn0.Attributes["PtX"].Value);
			double num2 = Convert.ToDouble(xn0.Attributes["PtY"].Value);
			double num3 = Convert.ToDouble(xn0.Attributes["PtZ"].Value);
			int beamId = Convert.ToInt32(xn0.Attributes["BeamId"].Value);
			int pipeId = Convert.ToInt32(xn0.Attributes["PipeId"].Value);
			double num4 = Convert.ToDouble(xn0.Attributes["Parameter1"].Value);
			double param = Convert.ToDouble(xn0.Attributes["Parameter2"].Value);
			OpeningStatus status = (OpeningStatus)Convert.ToInt32(xn0.Attributes["Status"].Value);
			SectionParameter sp;
			if ((int)Convert.ToDouble(xn0.Attributes["SectionType"].Value) == 1)
			{
				sp = new SectionParameter(num4);
			}
			else
			{
				sp = new SectionParameter(num4, param);
			}
			double num5 = Convert.ToDouble(xn0.Attributes["BoxParameter1"].Value);
			double param2 = Convert.ToDouble(xn0.Attributes["BoxParameter2"].Value);
			double param3 = Convert.ToDouble(xn0.Attributes["BoxParameter3"].Value);
			double param4 = Convert.ToDouble(xn0.Attributes["BoxParameter4"].Value);
			string remark = Convert.ToString(xn0.Attributes["Remark"].Value);
			BoxParameter bp;
			if (Convert.ToInt32(xn0.Attributes["BoxSectionType"].Value) == 1)
			{
				bp = new BoxParameter(num5);
			}
			else
			{
				bp = new BoxParameter(num5, param2, param3, param4);
			}
			CutType cType = (CutType)Convert.ToInt32(xn0.Attributes["CutType"].Value);
			HostType hType = (HostType)Convert.ToInt32(xn0.Attributes["HostType"].Value);
			string level = Convert.ToString(xn0.Attributes["Level"].Value);
			DbBeamToBeOpened dbBeamToBeOpened = new DbBeamToBeOpened(0, new XYZ(num, num2, num3), beamId, pipeId, sp, bp, cType, hType, level);
			dbBeamToBeOpened.Remark = remark;
			dbBeamToBeOpened.Status = status;
			dbBeamToBeOpened.Guid = Guid.Parse(XmlUtils.getGuid(xn0.Name));
			dbBeamToBeOpened.Date = DateTime.Parse(xn0.Attributes["Date"].Value);
			dbBeamToBeOpened.DrivePipeTypeName = xn0.Attributes["DrivePipeTypeName"].Value;
			dbBeamToBeOpened.AxisX = xn0.Attributes["AxisX"].Value;
			dbBeamToBeOpened.AxisY = xn0.Attributes["AxisY"].Value;
			dbBeamToBeOpened.MEPFileName = xn0.Attributes["MEPFileName"].Value;
			double num6 = Convert.ToDouble(xn0.Attributes["PipeStartPointX"].Value);
			double num7 = Convert.ToDouble(xn0.Attributes["PipeStartPointY"].Value);
			double num8 = Convert.ToDouble(xn0.Attributes["PipeStartPointZ"].Value);
			dbBeamToBeOpened.PipeStartPoint = new XYZ(num6, num7, num8);
			double num9 = Convert.ToDouble(xn0.Attributes["PipeEndPointX"].Value);
			double num10 = Convert.ToDouble(xn0.Attributes["PipeEndPointY"].Value);
			double num11 = Convert.ToDouble(xn0.Attributes["PipeEndPointZ"].Value);
			dbBeamToBeOpened.PipeEndPoint = new XYZ(num9, num10, num11);
			if (xn0.Attributes["OpeningID"] != null)
			{
				dbBeamToBeOpened.OpeningID = new ElementId(Convert.ToInt32(xn0.Attributes["OpeningID"].Value));
			}
			return dbBeamToBeOpened;
		}

		public static void AddWallOpening(string xmlPathName, DbWallToBeOpened opening)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(xmlPathName);
				foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, opening.MEPFileName, opening.IsValid))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "墙洞")
					{
						XmlElement xmlElement = xmlDocument.CreateElement("Id-" + opening.Guid.ToString());
						xmlElement.SetAttribute("PtX", opening.Location.X.ToString());
						xmlElement.SetAttribute("PtY", opening.Location.Y.ToString());
						xmlElement.SetAttribute("PtZ", opening.Location.Z.ToString());
						xmlElement.SetAttribute("WallId", opening.HostId.ToString());
						xmlElement.SetAttribute("Parameter1", opening.SectionParameter.Parameter1.ToString());
						xmlElement.SetAttribute("Parameter2", opening.SectionParameter.Parameter2.ToString());
						xmlElement.SetAttribute("SectionType", ((int)opening.SectionParameter.SectionType).ToString());
						xmlElement.SetAttribute("Status", ((int)opening.Status).ToString());
						xmlElement.SetAttribute("BoxParameter1", opening.BoxParameter.Parameter1.ToString());
						xmlElement.SetAttribute("BoxParameter2", opening.BoxParameter.Parameter2.ToString());
						xmlElement.SetAttribute("BoxParameter3", opening.BoxParameter.Parameter3.ToString());
						xmlElement.SetAttribute("BoxParameter4", opening.BoxParameter.Parameter4.ToString());
						xmlElement.SetAttribute("CutType", ((int)opening.CutType).ToString());
						xmlElement.SetAttribute("HostType", ((int)opening.HostType).ToString());
						xmlElement.SetAttribute("BoxSectionType", ((int)opening.BoxParameter.SectionType).ToString());
						xmlElement.SetAttribute("Level", opening.Level.ToString());
						xmlElement.SetAttribute("Remark", opening.Remark);
						xmlElement.SetAttribute("Date", opening.Date.ToString());
						xmlElement.SetAttribute("PipeId", opening.PipeId.ToString());
						xmlElement.SetAttribute("DrivePipeTypeName", opening.DrivePipeTypeName.ToString());
						xmlElement.SetAttribute("AxisX", opening.AxisX);
						xmlElement.SetAttribute("AxisY", opening.AxisY);
						xmlElement.SetAttribute("MEPFileName", opening.MEPFileName);
						xmlElement.SetAttribute("PipeStartPointX", opening.PipeStartPoint.X.ToString());
						xmlElement.SetAttribute("PipeStartPointY", opening.PipeStartPoint.Y.ToString());
						xmlElement.SetAttribute("PipeStartPointZ", opening.PipeStartPoint.Z.ToString());
						xmlElement.SetAttribute("PipeEndPointX", opening.PipeEndPoint.X.ToString());
						xmlElement.SetAttribute("PipeEndPointY", opening.PipeEndPoint.Y.ToString());
						xmlElement.SetAttribute("PipeEndPointZ", opening.PipeEndPoint.Z.ToString());
						if (opening.OpeningID != null)
						{
							xmlElement.SetAttribute("OpeningID", opening.OpeningID.ToString());
						}
						if (opening.vtWallOrient != null)
						{
							xmlElement.SetAttribute("vtWallOrientX", opening.vtWallOrient.X.ToString());
							xmlElement.SetAttribute("vtWallOrientY", opening.vtWallOrient.Y.ToString());
							xmlElement.SetAttribute("vtWallOrientZ", opening.vtWallOrient.Z.ToString());
						}
						xmlElement.SetAttribute("dWallWidth", opening.dWallWidth.ToString());
						xmlElement.SetAttribute("bEquipOpening", opening.bEquipOpening.ToString());
						xmlElement.SetAttribute("dOpeningThickness", opening.dOpeningThickness.ToString());
						xmlNode.AppendChild(xmlElement);
						break;
					}
				}
				xmlDocument.Save(xmlPathName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static void AddBeamOpening(string fileName, DbBeamToBeOpened opening)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, opening.MEPFileName, opening.IsValid))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "梁洞")
					{
						XmlElement xmlElement = xmlDocument.CreateElement("Id-" + opening.Guid.ToString());
						xmlElement.SetAttribute("PtX", opening.Location.X.ToString());
						xmlElement.SetAttribute("PtY", opening.Location.Y.ToString());
						xmlElement.SetAttribute("PtZ", opening.Location.Z.ToString());
						xmlElement.SetAttribute("BeamId", opening.HostId.ToString());
						xmlElement.SetAttribute("Parameter1", opening.SectionParameter.Parameter1.ToString());
						xmlElement.SetAttribute("Parameter2", opening.SectionParameter.Parameter2.ToString());
						xmlElement.SetAttribute("SectionType", ((int)opening.SectionParameter.SectionType).ToString());
						xmlElement.SetAttribute("Status", ((int)opening.Status).ToString());
						xmlElement.SetAttribute("BoxParameter1", opening.BoxParameter.Parameter1.ToString());
						xmlElement.SetAttribute("BoxParameter2", opening.BoxParameter.Parameter2.ToString());
						xmlElement.SetAttribute("BoxParameter3", opening.BoxParameter.Parameter3.ToString());
						xmlElement.SetAttribute("BoxParameter4", opening.BoxParameter.Parameter4.ToString());
						xmlElement.SetAttribute("CutType", ((int)opening.CutType).ToString());
						xmlElement.SetAttribute("HostType", ((int)opening.HostType).ToString());
						xmlElement.SetAttribute("BoxSectionType", ((int)opening.BoxParameter.SectionType).ToString());
						xmlElement.SetAttribute("Level", opening.Level.ToString());
						xmlElement.SetAttribute("Remark", opening.Remark);
						xmlElement.SetAttribute("Date", opening.Date.ToString());
						xmlElement.SetAttribute("PipeId", opening.PipeId.ToString());
						xmlElement.SetAttribute("DrivePipeTypeName", opening.DrivePipeTypeName.ToString());
						xmlElement.SetAttribute("AxisX", opening.AxisX);
						xmlElement.SetAttribute("AxisY", opening.AxisY);
						xmlElement.SetAttribute("MEPFileName", opening.MEPFileName);
						xmlElement.SetAttribute("PipeStartPointX", opening.PipeStartPoint.X.ToString());
						xmlElement.SetAttribute("PipeStartPointY", opening.PipeStartPoint.Y.ToString());
						xmlElement.SetAttribute("PipeStartPointZ", opening.PipeStartPoint.Z.ToString());
						xmlElement.SetAttribute("PipeEndPointX", opening.PipeEndPoint.X.ToString());
						xmlElement.SetAttribute("PipeEndPointY", opening.PipeEndPoint.Y.ToString());
						xmlElement.SetAttribute("PipeEndPointZ", opening.PipeEndPoint.Z.ToString());
						if (opening.OpeningID != null)
						{
							xmlElement.SetAttribute("OpeningID", opening.OpeningID.ToString());
						}
						xmlNode.AppendChild(xmlElement);
						break;
					}
				}
				xmlDocument.Save(fileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static void AddFloorOpening(string fileName, DbFloorToBeOpened opening)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, opening.MEPFileName, opening.IsValid))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "板洞")
					{
						XmlElement xmlElement = xmlDocument.CreateElement("Id-" + opening.Guid.ToString());
						xmlElement.SetAttribute("PtX", opening.Location.X.ToString());
						xmlElement.SetAttribute("PtY", opening.Location.Y.ToString());
						xmlElement.SetAttribute("PtZ", opening.Location.Z.ToString());
						xmlElement.SetAttribute("FloorId", opening.HostId.ToString());
						xmlElement.SetAttribute("Parameter1", opening.SectionParameter.Parameter1.ToString());
						xmlElement.SetAttribute("Parameter2", opening.SectionParameter.Parameter2.ToString());
						xmlElement.SetAttribute("SectionType", ((int)opening.SectionParameter.SectionType).ToString());
						xmlElement.SetAttribute("Status", ((int)opening.Status).ToString());
						xmlElement.SetAttribute("BoxParameter1", opening.BoxParameter.Parameter1.ToString());
						xmlElement.SetAttribute("BoxParameter2", opening.BoxParameter.Parameter2.ToString());
						xmlElement.SetAttribute("BoxParameter3", opening.BoxParameter.Parameter3.ToString());
						xmlElement.SetAttribute("BoxParameter4", opening.BoxParameter.Parameter4.ToString());
						xmlElement.SetAttribute("CutType", ((int)opening.CutType).ToString());
						xmlElement.SetAttribute("HostType", ((int)opening.HostType).ToString());
						xmlElement.SetAttribute("BoxSectionType", ((int)opening.BoxParameter.SectionType).ToString());
						xmlElement.SetAttribute("Level", opening.Level.ToString());
						xmlElement.SetAttribute("Remark", opening.Remark);
						xmlElement.SetAttribute("Date", opening.Date.ToString());
						xmlElement.SetAttribute("PipeId", opening.PipeId.ToString());
						xmlElement.SetAttribute("DrivePipeTypeName", opening.DrivePipeTypeName.ToString());
						xmlElement.SetAttribute("AxisX", opening.AxisX);
						xmlElement.SetAttribute("AxisY", opening.AxisY);
						xmlElement.SetAttribute("MEPFileName", opening.MEPFileName);
						xmlElement.SetAttribute("PipeStartPointX", opening.PipeStartPoint.X.ToString());
						xmlElement.SetAttribute("PipeStartPointY", opening.PipeStartPoint.Y.ToString());
						xmlElement.SetAttribute("PipeStartPointZ", opening.PipeStartPoint.Z.ToString());
						xmlElement.SetAttribute("PipeEndPointX", opening.PipeEndPoint.X.ToString());
						xmlElement.SetAttribute("PipeEndPointY", opening.PipeEndPoint.Y.ToString());
						xmlElement.SetAttribute("PipeEndPointZ", opening.PipeEndPoint.Z.ToString());
						if (opening.OpeningID != null)
						{
							xmlElement.SetAttribute("OpeningID", opening.OpeningID.ToString());
						}
						xmlElement.SetAttribute("PipeConnAxisXOnSectionPlaneAng", opening.PipeConnAxisXOnSectionPlaneAng.ToString());
						xmlNode.AppendChild(xmlElement);
						break;
					}
				}
				xmlDocument.Save(fileName);
			}
			catch
			{
			}
		}

		public static void UpdateWallOpeningToXML(string fileName, DbWallToBeOpened opening)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, opening.MEPFileName, opening.IsValid))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "墙洞")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							if (xmlNode2.Name == "Id-" + opening.Guid.ToString())
							{
								xmlNode2.Attributes["PtX"].Value = opening.Location.X.ToString();
								xmlNode2.Attributes["PtY"].Value = opening.Location.Y.ToString();
								xmlNode2.Attributes["PtZ"].Value = opening.Location.Z.ToString();
								xmlNode2.Attributes["WallId"].Value = opening.HostId.ToString();
								xmlNode2.Attributes["Parameter1"].Value = opening.SectionParameter.Parameter1.ToString();
								xmlNode2.Attributes["Parameter2"].Value = opening.SectionParameter.Parameter2.ToString();
								xmlNode2.Attributes["SectionType"].Value = ((int)opening.SectionParameter.SectionType).ToString();
								xmlNode2.Attributes["Status"].Value = ((int)opening.Status).ToString();
								xmlNode2.Attributes["BoxParameter1"].Value = opening.BoxParameter.Parameter1.ToString();
								xmlNode2.Attributes["BoxParameter2"].Value = opening.BoxParameter.Parameter2.ToString();
								xmlNode2.Attributes["BoxParameter3"].Value = opening.BoxParameter.Parameter3.ToString();
								xmlNode2.Attributes["BoxParameter4"].Value = opening.BoxParameter.Parameter4.ToString();
								xmlNode2.Attributes["CutType"].Value = ((int)opening.CutType).ToString();
								xmlNode2.Attributes["HostType"].Value = ((int)opening.HostType).ToString();
								xmlNode2.Attributes["BoxSectionType"].Value = ((int)opening.BoxParameter.SectionType).ToString();
								xmlNode2.Attributes["Level"].Value = opening.Level;
								xmlNode2.Attributes["Remark"].Value = opening.Remark;
								xmlNode2.Attributes["DrivePipeTypeName"].Value = opening.DrivePipeTypeName.ToString();
								xmlNode2.Attributes["AxisX"].Value = opening.AxisX;
								xmlNode2.Attributes["AxisY"].Value = opening.AxisY;
								xmlNode2.Attributes["MEPFileName"].Value = opening.MEPFileName;
								xmlNode2.Attributes["PipeStartPointX"].Value = opening.PipeStartPoint.X.ToString();
								xmlNode2.Attributes["PipeStartPointY"].Value = opening.PipeStartPoint.Y.ToString();
								xmlNode2.Attributes["PipeStartPointZ"].Value = opening.PipeStartPoint.Z.ToString();
								xmlNode2.Attributes["PipeEndPointX"].Value = opening.PipeEndPoint.X.ToString();
								xmlNode2.Attributes["PipeEndPointY"].Value = opening.PipeEndPoint.Y.ToString();
								xmlNode2.Attributes["PipeEndPointZ"].Value = opening.PipeEndPoint.Z.ToString();
								if (opening.OpeningID != null)
								{
									xmlNode2.Attributes["OpeningID"].Value = opening.OpeningID.ToString();
								}
								if (opening.vtWallOrient != null)
								{
									xmlNode2.Attributes["vtWallOrientX"].Value = opening.vtWallOrient.X.ToString();
									xmlNode2.Attributes["vtWallOrientY"].Value = opening.vtWallOrient.Y.ToString();
									xmlNode2.Attributes["vtWallOrientZ"].Value = opening.vtWallOrient.Z.ToString();
								}
								xmlNode2.Attributes["dWallWidth"].Value = opening.dWallWidth.ToString();
								xmlNode2.Attributes["bEquipOpening"].Value = opening.bEquipOpening.ToString();
								xmlNode2.Attributes["dOpeningThickness"].Value = opening.dOpeningThickness.ToString();
								xmlDocument.Save(fileName);
								return;
							}
						}
					}
				}
			}
			catch
			{
			}
		}

		public static void UpdateFloorOpeningToXML(string fileName, DbFloorToBeOpened opening)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, opening.MEPFileName, opening.IsValid))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "板洞")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							if (xmlNode2.Name == "Id-" + opening.Guid.ToString())
							{
								xmlNode2.Attributes["PtX"].Value = opening.Location.X.ToString();
								xmlNode2.Attributes["PtY"].Value = opening.Location.Y.ToString();
								xmlNode2.Attributes["PtZ"].Value = opening.Location.Z.ToString();
								xmlNode2.Attributes["FloorId"].Value = opening.HostId.ToString();
								xmlNode2.Attributes["Parameter1"].Value = opening.SectionParameter.Parameter1.ToString();
								xmlNode2.Attributes["Parameter2"].Value = opening.SectionParameter.Parameter2.ToString();
								xmlNode2.Attributes["SectionType"].Value = ((int)opening.SectionParameter.SectionType).ToString();
								xmlNode2.Attributes["Status"].Value = ((int)opening.Status).ToString();
								xmlNode2.Attributes["BoxParameter1"].Value = opening.BoxParameter.Parameter1.ToString();
								xmlNode2.Attributes["BoxParameter2"].Value = opening.BoxParameter.Parameter2.ToString();
								xmlNode2.Attributes["BoxParameter3"].Value = opening.BoxParameter.Parameter3.ToString();
								xmlNode2.Attributes["BoxParameter4"].Value = opening.BoxParameter.Parameter4.ToString();
								xmlNode2.Attributes["CutType"].Value = ((int)opening.CutType).ToString();
								xmlNode2.Attributes["HostType"].Value = ((int)opening.HostType).ToString();
								xmlNode2.Attributes["BoxSectionType"].Value = ((int)opening.BoxParameter.SectionType).ToString();
								xmlNode2.Attributes["Level"].Value = opening.Level;
								xmlNode2.Attributes["Remark"].Value = opening.Remark;
								xmlNode2.Attributes["DrivePipeTypeName"].Value = opening.DrivePipeTypeName.ToString();
								xmlNode2.Attributes["AxisX"].Value = opening.AxisX;
								xmlNode2.Attributes["AxisY"].Value = opening.AxisY;
								xmlNode2.Attributes["MEPFileName"].Value = opening.MEPFileName;
								xmlNode2.Attributes["PipeStartPointX"].Value = opening.PipeStartPoint.X.ToString();
								xmlNode2.Attributes["PipeStartPointY"].Value = opening.PipeStartPoint.Y.ToString();
								xmlNode2.Attributes["PipeStartPointZ"].Value = opening.PipeStartPoint.Z.ToString();
								xmlNode2.Attributes["PipeEndPointX"].Value = opening.PipeEndPoint.X.ToString();
								xmlNode2.Attributes["PipeEndPointY"].Value = opening.PipeEndPoint.Y.ToString();
								xmlNode2.Attributes["PipeEndPointZ"].Value = opening.PipeEndPoint.Z.ToString();
								if (opening.OpeningID != null)
								{
									xmlNode2.Attributes["OpeningID"].Value = opening.OpeningID.ToString();
								}
								xmlNode2.Attributes["PipeConnAxisXOnSectionPlaneAng"].Value = opening.PipeConnAxisXOnSectionPlaneAng.ToString();
								xmlDocument.Save(fileName);
								return;
							}
						}
					}
				}
			}
			catch
			{
			}
		}

		public static void UpdateBeamOpeningToXML(string fileName, DbBeamToBeOpened opening)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in XmlUtils.GetXmlItemNodeByFileName(xmlDocument, opening.MEPFileName, opening.IsValid))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "梁洞")
					{
						foreach (object obj2 in xmlNode.ChildNodes)
						{
							XmlNode xmlNode2 = (XmlNode)obj2;
							if (xmlNode2.Name == "Id-" + opening.Guid.ToString())
							{
								xmlNode2.Attributes["PtX"].Value = opening.Location.X.ToString();
								xmlNode2.Attributes["PtY"].Value = opening.Location.Y.ToString();
								xmlNode2.Attributes["PtZ"].Value = opening.Location.Z.ToString();
								xmlNode2.Attributes["BeamId"].Value = opening.HostId.ToString();
								xmlNode2.Attributes["Parameter1"].Value = opening.SectionParameter.Parameter1.ToString();
								xmlNode2.Attributes["Parameter2"].Value = opening.SectionParameter.Parameter2.ToString();
								xmlNode2.Attributes["SectionType"].Value = ((int)opening.SectionParameter.SectionType).ToString();
								xmlNode2.Attributes["Status"].Value = ((int)opening.Status).ToString();
								xmlNode2.Attributes["BoxParameter1"].Value = opening.BoxParameter.Parameter1.ToString();
								xmlNode2.Attributes["BoxParameter2"].Value = opening.BoxParameter.Parameter2.ToString();
								xmlNode2.Attributes["BoxParameter3"].Value = opening.BoxParameter.Parameter3.ToString();
								xmlNode2.Attributes["BoxParameter4"].Value = opening.BoxParameter.Parameter4.ToString();
								xmlNode2.Attributes["CutType"].Value = ((int)opening.CutType).ToString();
								xmlNode2.Attributes["HostType"].Value = ((int)opening.HostType).ToString();
								xmlNode2.Attributes["BoxSectionType"].Value = ((int)opening.BoxParameter.SectionType).ToString();
								xmlNode2.Attributes["Level"].Value = opening.Level;
								xmlNode2.Attributes["Remark"].Value = opening.Remark;
								xmlNode2.Attributes["DrivePipeTypeName"].Value = opening.DrivePipeTypeName.ToString();
								xmlNode2.Attributes["AxisX"].Value = opening.AxisX;
								xmlNode2.Attributes["AxisY"].Value = opening.AxisY;
								xmlNode2.Attributes["MEPFileName"].Value = opening.MEPFileName;
								xmlNode2.Attributes["PipeStartPointX"].Value = opening.PipeStartPoint.X.ToString();
								xmlNode2.Attributes["PipeStartPointY"].Value = opening.PipeStartPoint.Y.ToString();
								xmlNode2.Attributes["PipeStartPointZ"].Value = opening.PipeStartPoint.Z.ToString();
								xmlNode2.Attributes["PipeEndPointX"].Value = opening.PipeEndPoint.X.ToString();
								xmlNode2.Attributes["PipeEndPointY"].Value = opening.PipeEndPoint.Y.ToString();
								xmlNode2.Attributes["PipeEndPointZ"].Value = opening.PipeEndPoint.Z.ToString();
								if (opening.OpeningID != null)
								{
									xmlNode2.Attributes["OpeningID"].Value = opening.OpeningID.ToString();
								}
								xmlDocument.Save(fileName);
								return;
							}
						}
					}
				}
			}
			catch
			{
			}
		}

		public static string GetDataFileName(Autodesk.Revit.DB.Document doc)
		{
			FileInfo fileInfo = new FileInfo(doc.PathName);
			int num = fileInfo.FullName.LastIndexOf(".");
			int num2 = fileInfo.FullName.LastIndexOf("\\");
			return fileInfo.FullName.Substring(num2 + 1, num - num2 - 1) + "_Data.xml";
		}

		private static string getGuid(string xmlValue)
		{
			string result;
			try
			{
				int num = xmlValue.IndexOf("-");
				result = xmlValue.Substring(num + 1, xmlValue.Length - num - 1);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = "";
			}
			return result;
		}

		public static void SetAutoCasing(string fileName, bool autoCreate)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "添加套管")
					{
						((XmlElement)xmlNode).SetAttribute("AutoCreate", autoCreate.ToString());
						break;
					}
				}
				xmlDocument.Save(fileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static void SetCasingType(string fileName, CasingType type)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				foreach (object obj in xmlDocument.SelectSingleNode("协同数据"))
				{
					XmlNode xmlNode = (XmlNode)obj;
					if (xmlNode.Name == "添加套管")
					{
						XmlElement xmlElement = (XmlElement)xmlNode;
						string name = "CasingType";
						int num = (int)type;
						xmlElement.SetAttribute(name, num.ToString());
						break;
					}
				}
				xmlDocument.Save(fileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
	}
}
